package cn.alise.qdp.service.api;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import jxl.Sheet;
import jxl.Workbook;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.alise.consts.ConfigConsts;
import cn.alise.enums.EnumClass;
import cn.alise.exception.ServiceException;
import cn.alise.qdp.model.category.CategoryImg;
import cn.alise.qdp.model.category.CategoryPrice;
import cn.alise.qdp.model.category.CategoryVideo;
import cn.alise.qdp.model.common.JsonParam;
import cn.alise.qdp.model.common.ResultEntity;
import cn.alise.qdp.model.inventory.Inventory;
import cn.alise.qdp.model.other.Other;
import cn.alise.qdp.model.wxuser.Wxuser;
import cn.alise.qdp.service.category.CategoryImgService;
import cn.alise.qdp.service.category.CategoryPriceService;
import cn.alise.qdp.service.category.CategoryVideoService;
import cn.alise.qdp.service.common.RedisService;
import cn.alise.qdp.service.common.UploadService;
import cn.alise.qdp.service.inventory.InventoryService;
import cn.alise.qdp.service.other.OtherService;
import cn.alise.qdp.service.wxuser.WxuserService;
import cn.alise.util.DateUtil;
import cn.alise.util.FileUtil;
import cn.alise.util.MD5Util;
import common.Logger;

@Service("apiService")
@Transactional(rollbackFor = Exception.class)
public class ApiService {
	
	protected Logger logger = Logger.getLogger(this.getClass());
	
	@Resource(name = "uploadService")
	private UploadService uploadService;
	
	@Resource(name = "wxuserService")
	private WxuserService wxuserService;
	
	@Resource(name = "redisService")
	private RedisService redisService;
	
	@Resource(name = "categoryImgService")
	private CategoryImgService categoryImgService;
	
	@Resource(name = "categoryVideoService")
	private CategoryVideoService categoryVideoService;
	
	@Resource(name = "categoryPriceService")
	private CategoryPriceService categoryPriceService;
	
	@Resource(name = "otherService")
	private OtherService otherService;
	
	@Resource(name = "inventoryService")
	private InventoryService inventoryService;
	
	@Value("${category.img.limit:0}")
	private String CATEGORY_IMG_LIMIT;
	
	@Value("${category.video.limit:0}")
	private String CATEGORY_VIDEO_LIMIT;
	
	@Value("${category.price.limit:0}")
	private String CATEGORY_PRICE_LIMIT;
	
	@Value("${other.img.limit:0}")
	private String OTHER_IMG_LIMIT;
	
	@Value("${app.version:0}")
	private String APP_VERSION;
	
	/**
	 * 登录
	 */
	public ResultEntity login(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、 判断账号是否存在
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getMobile());
	    	wxuser = this.wxuserService.get(wxuser);
	    	
	    	if (wxuser == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、判断密码是否正确
	    	String password = MD5Util.MD5(jp.getPassword() + wxuser.getMask());
	    	if (!wxuser.getPassword().equals(password)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.PASSWORD_ERROR.getKey(), EnumClass.ResultCodeType.PASSWORD_ERROR.getValue());
	    	}
	    	
	    	// 3、判断是否过期
	    	long nowTime = System.currentTimeMillis();
	    	if (wxuser.getExpireTime() != null && wxuser.getExpireTime().getTime() < nowTime) {
	    		throw new ServiceException(EnumClass.ResultCodeType.EXPIRE_TIME.getKey(), EnumClass.ResultCodeType.EXPIRE_TIME.getValue());
	    	}
	    	
	    	// 4、生成token
	    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	wxuser.setExpireTimeStr(df.format(wxuser.getExpireTime()));
	    	String token = this.redisService.setTokenRedis(wxuser);
	    	
			// 5、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setToken(token);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * APP版本比对
	 */
	public ResultEntity checkAppVersion(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取当前最新APP版本
	    	String appVersionNow = APP_VERSION;
		    
	    	// 2、比对是否为最新版本
	    	if (Integer.parseInt(jp.getAppVersion()) < Integer.parseInt(appVersionNow)) {
	    		resultEntity.setAppDownloadUrl(ConfigConsts.HTTP_IMG_URL + appVersionNow + ".apk");
	    	}
			
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 登录注销
	 */
	public ResultEntity logout(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、清除token
		    this.redisService.clearTokenRedis(jp.getMobile());
	    	
			// 2、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取个人信息
	 */
	public ResultEntity getUserInfo(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、 判断账号是否存在
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getMobile());
	    	wxuser = this.wxuserService.get(wxuser);
	    	
	    	if (wxuser == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、判断是否过期
	    	long nowTime = System.currentTimeMillis();
	    	if (wxuser.getExpireTime() != null && wxuser.getExpireTime().getTime() < nowTime) {
	    		// 注销登录，清除token
			    this.redisService.clearTokenRedis(jp.getMobile());
	    		throw new ServiceException(EnumClass.ResultCodeType.EXPIRE_TIME.getKey(), EnumClass.ResultCodeType.EXPIRE_TIME.getValue());
	    	}
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			wxuser.setPassword(null);
			wxuser.setMask(null);
			wxuser.setCreateTime(null);
			resultEntity.setWxuser(wxuser);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 编辑个人信息
	 */
	public ResultEntity editUserInfo(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、 判断账号是否存在
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getMobile());
	    	wxuser = this.wxuserService.get(wxuser);
	    	
	    	if (wxuser == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、编辑信息
	    	Wxuser edit = new Wxuser();
	    	edit.setMobile(jp.getMobile());
	    	if (!StringUtils.isEmpty(jp.getName())) {
	    		edit.setName(jp.getName());
	    	}
	    	if (!StringUtils.isEmpty(jp.getOrganName())) {
	    		edit.setOrganName(jp.getOrganName());
	    	}
	    	if (!StringUtils.isEmpty(jp.getSituation())) {
	    		edit.setSituation(jp.getSituation());
	    	}
	    	this.wxuserService.update(edit);
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 密码修改
	 */
	public ResultEntity editPwd(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、 判断账号是否存在
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getMobile());
	    	wxuser = this.wxuserService.get(wxuser);
	    	
	    	if (wxuser == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、判断旧密码是否正确
	    	String password = MD5Util.MD5(jp.getOldPassword() + wxuser.getMask());
	    	if (!wxuser.getPassword().equals(password)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.PASSWORD_ERROR.getKey(), EnumClass.ResultCodeType.PASSWORD_ERROR.getValue());
	    	}
	    	
	    	// 3、设置新密码
    		// 随机6位数作为MASK
    		String mask = (int)(Math.random() * 1000000) + "";
    		String psd = MD5Util.MD5(jp.getNewPassword() + mask);
    		Wxuser edit = new Wxuser();
	    	edit.setMobile(jp.getMobile());
	    	edit.setPassword(psd);
    		edit.setMask(mask);
    		this.wxuserService.update(edit);
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取类别图片库
	 */
	public ResultEntity getCategoryImgs(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取类别图片库
	    	CategoryImg categoryImg = new CategoryImg();
	    	categoryImg.setMobile(jp.getMobile());
	    	categoryImg.setType(jp.getType());
	    	List<CategoryImg> categoryImgList = this.categoryImgService.list(categoryImg);
	    	
	    	// 2、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 2、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setCategoryImgList(categoryImgList);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别图片库新增图片
	 */
	public ResultEntity uploadCategoryImg(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否超出大小
	    	CategoryImg categoryImg = new CategoryImg();
	    	categoryImg.setMobile(jp.getMobile());
	    	categoryImg.setType(jp.getType());
	    	int total = this.categoryImgService.total(categoryImg);
	    	
	    	if (total >= Integer.parseInt(CATEGORY_IMG_LIMIT)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.IMG_LIMIT_ERROR.getKey(), EnumClass.ResultCodeType.IMG_LIMIT_ERROR.getValue());
	    	}
	    	
	    	// 2、保存图片
	    	String imgUrl = "";
	    	if (!StringUtils.isEmpty(jp.getImg())) {
	    		imgUrl = this.uploadService.saveImg(jp.getImg(), jp.getMobile());
	    	}
	    	
	    	// 3、新增
	    	categoryImg.setId(UUID.randomUUID().toString().replaceAll("-", ""));
	    	categoryImg.setImgUrl(imgUrl);
	    	if (jp.getMobile().equals("13999999999")) {
	    		categoryImg.setDefaultFlag(1);
	    	} else {
	    		categoryImg.setDefaultFlag(0);
	    	}
	    	this.categoryImgService.save(categoryImg);
	    	
	    	// 4、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 5、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setCategoryImg(categoryImg);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别图片库删除图片
	 */
	public ResultEntity delCategoryImg(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否存在
	    	CategoryImg categoryImg = new CategoryImg();
	    	categoryImg.setMobile(jp.getMobile());
	    	categoryImg.setId(jp.getId());
	    	categoryImg = this.categoryImgService.get(categoryImg);
	    	
	    	if (categoryImg == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.IMG_NOT_EXIST.getKey(), EnumClass.ResultCodeType.IMG_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、删除数据
	    	CategoryImg del = new CategoryImg();
	    	del.setMobile(jp.getMobile());
	    	del.setId(jp.getId());
	    	this.categoryImgService.delete(del);
	    	
	    	// 3、删除图片(默认的不可删除)
	    	if (categoryImg.getDefaultFlag() == 0) {
	    		String localImgUrl = ConfigConsts.lacp_SAVEIMG_PATH;
	    		FileUtil.deleteFile(localImgUrl + "/" + categoryImg.getImgUrl());
	    	}
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取类别视频库
	 */
	public ResultEntity getCategoryVideos(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取类别视频库
	    	CategoryVideo categoryVideo = new CategoryVideo();
	    	categoryVideo.setMobile(jp.getMobile());
	    	categoryVideo.setType(jp.getType());
	    	List<CategoryVideo> categoryVideoList = this.categoryVideoService.list(categoryVideo);
	    	
	    	// 2、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setCategoryVideoList(categoryVideoList);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别视频库新增视频
	 */
	public ResultEntity uploadCategoryVideo(JsonParam jp, HttpServletRequest request) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否超出大小
	    	CategoryVideo categoryVideo = new CategoryVideo();
	    	categoryVideo.setMobile(jp.getMobile());
	    	categoryVideo.setType(jp.getType());
	    	int total = this.categoryVideoService.total(categoryVideo);
	    	
	    	if (total >= Integer.parseInt(CATEGORY_VIDEO_LIMIT)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.VIDEO_LIMIT_ERROR.getKey(), EnumClass.ResultCodeType.VIDEO_LIMIT_ERROR.getValue());
	    	}
	    	
	    	// 2、保存视频
	    	String videoUrl = "";
		    if (request instanceof MultipartHttpServletRequest) {
		    	videoUrl = uploadService.saveVideo(request);
			}
	    	
	    	// 3、新增
		    categoryVideo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		    categoryVideo.setVideoUrl(videoUrl);
		    if (jp.getMobile().equals("13999999999")) {
		    	categoryVideo.setDefaultFlag(1);
	    	} else {
	    		categoryVideo.setDefaultFlag(0);
	    	}
		    categoryVideo.setTitle(jp.getTitle());
	    	this.categoryVideoService.save(categoryVideo);
	    	
	    	// 4、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 5、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setCategoryVideo(categoryVideo);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别视频库删除视频
	 */
	public ResultEntity delCategoryVideo(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否存在
	    	CategoryVideo categoryVideo = new CategoryVideo();
	    	categoryVideo.setMobile(jp.getMobile());
	    	categoryVideo.setId(jp.getId());
	    	categoryVideo = this.categoryVideoService.get(categoryVideo);
	    	
	    	if (categoryVideo == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.VIDEO_NOT_EXIST.getKey(), EnumClass.ResultCodeType.VIDEO_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、删除数据
	    	CategoryVideo del = new CategoryVideo();
	    	del.setMobile(jp.getMobile());
	    	del.setId(jp.getId());
	    	this.categoryVideoService.delete(del);
	    	
	    	// 3、删除视频(默认的不可删除)
	    	if (categoryVideo.getDefaultFlag() == 0) {
	    		String localImgUrl = ConfigConsts.lacp_SAVEIMG_PATH;
	    		FileUtil.deleteFile(localImgUrl + "/" + categoryVideo.getVideoUrl());
	    	}
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取类别价格库
	 */
	public ResultEntity getCategoryPrices(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取类别价格库
	    	CategoryPrice categoryPrice = new CategoryPrice();
	    	categoryPrice.setMobile(jp.getMobile());
	    	categoryPrice.setType(jp.getType());
	    	List<CategoryPrice> categoryPriceList = this.categoryPriceService.list(categoryPrice);
	    	
	    	// 2、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setCategoryPriceList(categoryPriceList);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取价格详情
	 */
	public ResultEntity getPriceDetail(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、通过ID获取价格详情
	    	CategoryPrice categoryPrice = new CategoryPrice();
	    	categoryPrice.setId(jp.getId());
	    	categoryPrice = this.categoryPriceService.get(categoryPrice);
	    	
	    	// 2、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setCategoryPrice(categoryPrice);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别价格库新增价格
	 */
	public ResultEntity uploadCategoryPrice(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否超出大小
	    	CategoryPrice categoryPrice = new CategoryPrice();
	    	categoryPrice.setMobile(jp.getMobile());
	    	categoryPrice.setType(jp.getType());
	    	int total = this.categoryPriceService.total(categoryPrice);
	    	
	    	if (total >= Integer.parseInt(CATEGORY_PRICE_LIMIT)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.PRICE_LIMIT_ERROR.getKey(), EnumClass.ResultCodeType.PRICE_LIMIT_ERROR.getValue());
	    	}
	    	
	    	// 2、保存图片
	    	String imgUrl = "";
	    	if (!StringUtils.isEmpty(jp.getImg())) {
	    		imgUrl = this.uploadService.saveImg(jp.getImg(), jp.getMobile());
	    	}
	    	
	    	// 3、新增
	    	categoryPrice.setId(UUID.randomUUID().toString().replaceAll("-", ""));
	    	categoryPrice.setImgUrl(imgUrl);
	    	if (jp.getMobile().equals("13999999999")) {
	    		categoryPrice.setDefaultFlag(1);
	    	} else {
	    		categoryPrice.setDefaultFlag(0);
	    	}
	    	categoryPrice.setName(jp.getName());
	    	categoryPrice.setFeature(jp.getFeature());
	    	categoryPrice.setCountry(jp.getCountry());
	    	categoryPrice.setWarranty(jp.getWarranty());
	    	categoryPrice.setTincture(jp.getTincture());
	    	categoryPrice.setBeauty(jp.getBeauty());
	    	categoryPrice.setWear(jp.getWear());
	    	if (!StringUtils.isEmpty(jp.getAdvantage1())) {
	    		categoryPrice.setAdvantage1(jp.getAdvantage1());
	    	} else {
	    		categoryPrice.setAdvantage1(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getAdvantage2())) {
	    		categoryPrice.setAdvantage2(jp.getAdvantage2());
	    	} else {
	    		categoryPrice.setAdvantage2(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getAdvantage3())) {
	    		categoryPrice.setAdvantage3(jp.getAdvantage3());
	    	} else {
	    		categoryPrice.setAdvantage3(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getAdvantage4())) {
	    		categoryPrice.setAdvantage4(jp.getAdvantage4());
	    	} else {
	    		categoryPrice.setAdvantage4(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getInferior1())) {
	    		categoryPrice.setInferior1(jp.getInferior1());
	    	} else {
	    		categoryPrice.setInferior1(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getInferior2())) {
	    		categoryPrice.setInferior2(jp.getInferior2());
	    	} else {
	    		categoryPrice.setInferior2(" ");
	    	}
	    	categoryPrice.setPrice(jp.getPrice());
	    	categoryPrice.setPriceDesc(jp.getPriceDesc());
	    	categoryPrice.setSelectNum(jp.getSelectNum());
	    	categoryPrice.setSortOrder(jp.getSortOrder());
	    	
	    	this.categoryPriceService.save(categoryPrice);
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别价格库编辑价格
	 */
	public ResultEntity editCategoryPrice(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断该价格是否存在
	    	CategoryPrice judge = new CategoryPrice();
	    	judge.setId(jp.getId());
	    	judge = this.categoryPriceService.get(judge);
	    	
	    	if (judge == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.PRICE_NOT_EXIST.getKey(), EnumClass.ResultCodeType.PRICE_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、图片修改
	    	CategoryPrice categoryPrice = new CategoryPrice();
	    	String imgUrl = "";
	    	if (!StringUtils.isEmpty(jp.getImg())) {
	    		// 先删除原有图片
	    		if (judge.getDefaultFlag() == 0) {
		    		String localImgUrl = ConfigConsts.lacp_SAVEIMG_PATH;
		    		FileUtil.deleteFile(localImgUrl + "/" + judge.getImgUrl());
		    	}
	    		// 保存图片
	    		imgUrl = this.uploadService.saveImg(jp.getImg(), jp.getMobile());
	    		categoryPrice.setImgUrl(imgUrl);
	    	}
	    	
	    	// 3、数据修改
	    	categoryPrice.setId(jp.getId());
	    	categoryPrice.setName(jp.getName());
	    	categoryPrice.setFeature(jp.getFeature());
	    	categoryPrice.setCountry(jp.getCountry());
	    	categoryPrice.setWarranty(jp.getWarranty());
	    	categoryPrice.setTincture(jp.getTincture());
	    	categoryPrice.setBeauty(jp.getBeauty());
	    	categoryPrice.setWear(jp.getWear());
	    	if (!StringUtils.isEmpty(jp.getAdvantage1())) {
	    		categoryPrice.setAdvantage1(jp.getAdvantage1());
	    	} else {
	    		categoryPrice.setAdvantage1(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getAdvantage2())) {
	    		categoryPrice.setAdvantage2(jp.getAdvantage2());
	    	} else {
	    		categoryPrice.setAdvantage2(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getAdvantage3())) {
	    		categoryPrice.setAdvantage3(jp.getAdvantage3());
	    	} else {
	    		categoryPrice.setAdvantage3(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getAdvantage4())) {
	    		categoryPrice.setAdvantage4(jp.getAdvantage4());
	    	} else {
	    		categoryPrice.setAdvantage4(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getInferior1())) {
	    		categoryPrice.setInferior1(jp.getInferior1());
	    	} else {
	    		categoryPrice.setInferior1(" ");
	    	}
	    	if (!StringUtils.isEmpty(jp.getInferior2())) {
	    		categoryPrice.setInferior2(jp.getInferior2());
	    	} else {
	    		categoryPrice.setInferior2(" ");
	    	}
	    	categoryPrice.setPrice(jp.getPrice());
	    	categoryPrice.setPriceDesc(jp.getPriceDesc());
	    	categoryPrice.setSelectNum(jp.getSelectNum());
	    	categoryPrice.setSortOrder(jp.getSortOrder());
	    	
	    	this.categoryPriceService.update(categoryPrice);
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 类别价格库删除价格
	 */
	public ResultEntity delCategoryPrice(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否存在
	    	CategoryPrice categoryPrice = new CategoryPrice();
	    	categoryPrice.setMobile(jp.getMobile());
	    	categoryPrice.setId(jp.getId());
	    	categoryPrice = this.categoryPriceService.get(categoryPrice);
	    	
	    	if (categoryPrice == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.PRICE_NOT_EXIST.getKey(), EnumClass.ResultCodeType.PRICE_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、删除数据
	    	CategoryPrice del = new CategoryPrice();
	    	del.setMobile(jp.getMobile());
	    	del.setId(jp.getId());
	    	this.categoryPriceService.delete(del);
	    	
	    	// 3、删除图片(默认的不可删除)
	    	if (categoryPrice.getDefaultFlag() == 0) {
	    		String localImgUrl = ConfigConsts.lacp_SAVEIMG_PATH;
	    		FileUtil.deleteFile(localImgUrl + "/" + categoryPrice.getImgUrl());
	    	}
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取其他图片库
	 */
	public ResultEntity getOtherImgs(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取其他图片库
	    	Other other = new Other();
	    	other.setMobile(jp.getMobile());
	    	other.setType(jp.getType());
	    	List<Other> otherList = this.otherService.list(other);
	    	
	    	// 2、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setOtherList(otherList);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 其他图片库新增图片
	 */
	public ResultEntity uploadOtherImg(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否超出大小
	    	Other other = new Other();
	    	other.setMobile(jp.getMobile());
	    	other.setType(jp.getType());
	    	int total = this.otherService.total(other);
	    	
	    	if (total >= Integer.parseInt(OTHER_IMG_LIMIT)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.IMG_LIMIT_ERROR.getKey(), EnumClass.ResultCodeType.IMG_LIMIT_ERROR.getValue());
	    	}
	    	
	    	// 2、保存图片
	    	String imgUrl = "";
	    	if (!StringUtils.isEmpty(jp.getImg())) {
	    		imgUrl = this.uploadService.saveImg(jp.getImg(), jp.getMobile());
	    	}
	    	
	    	// 3、新增
	    	other.setId(UUID.randomUUID().toString().replaceAll("-", ""));
	    	other.setImgUrl(imgUrl);
	    	this.otherService.save(other);
	    	
	    	// 4、获取图片访问路径
	    	String httpImgUrl = ConfigConsts.HTTP_IMG_URL;
	    	
			// 5、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setOther(other);
			resultEntity.setHttpImgUrl(httpImgUrl);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 其他图片库删除图片
	 */
	public ResultEntity delOtherImg(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否存在
	    	Other other = new Other();
	    	other.setMobile(jp.getMobile());
	    	other.setId(jp.getId());
	    	other = this.otherService.get(other);
	    	
	    	if (other == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.IMG_NOT_EXIST.getKey(), EnumClass.ResultCodeType.IMG_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、删除数据
	    	Other del = new Other();
	    	del.setMobile(jp.getMobile());
	    	del.setId(jp.getId());
	    	this.otherService.delete(del);
	    	
	    	// 3、删除图片
    		String localImgUrl = ConfigConsts.lacp_SAVEIMG_PATH;
    		FileUtil.deleteFile(localImgUrl + "/" + other.getImgUrl());
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取清单
	 */
	public ResultEntity getInventory(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取清单列表
	    	Inventory inventory = new Inventory();
	    	inventory.setMobile(jp.getMobile());
	    	List<Inventory> inventoryList = this.inventoryService.list(inventory);
	    	
			// 2、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setInventoryList(inventoryList);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 导入清单
	 */
	public ResultEntity importInventory(JsonParam jp, HttpServletRequest request) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断账号是否存在
	    	Wxuser judge = new Wxuser();
	    	judge.setMobile(jp.getMobile());
	    	judge = this.wxuserService.get(judge);
	    	
	    	if (judge == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、删除原有清单数据
	    	Inventory del = new Inventory();
	    	del.setMobile(jp.getMobile());
	    	this.inventoryService.delete(del);
	    	
	    	// 3、获取文件并导入
	    	if (!(request instanceof MultipartHttpServletRequest)) {
	    		throw new ServiceException(EnumClass.ResultCodeType.IMPORT_FILE_ERROR.getKey(), EnumClass.ResultCodeType.IMPORT_FILE_ERROR.getValue());
			}
			
			MultipartHttpServletRequest multipartHttpRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multipartHttpRequest.getFileNames();
			
			while (iter.hasNext()) {
				
				// 获取文件
				MultipartFile file = multipartHttpRequest.getFile(iter.next());
				if (file == null) {
					throw new ServiceException(EnumClass.ResultCodeType.IMPORT_FILE_ERROR.getKey(), EnumClass.ResultCodeType.IMPORT_FILE_ERROR.getValue());
				}
				
				String url = ConfigConsts.lacp_SAVEIMG_PATH;
				if (!StringUtils.isEmpty(file.getOriginalFilename())) {
					
					String realPath = url + "excel/" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
					File tmpFile = new File(realPath);
					if (!tmpFile.getParentFile().exists()) {
						tmpFile.getParentFile().mkdirs();
					}
					try {
						// 写入文件到临时目录
						file.transferTo(tmpFile);
					
						// 开始导入
						this.importData(tmpFile, jp.getMobile());
					} catch (Exception e) {
						logger.error("导入数据异常！", e);
						throw new ServiceException(EnumClass.ResultCodeType.IMPORT_FILE_ERROR.getKey(), EnumClass.ResultCodeType.IMPORT_FILE_ERROR.getValue());
					}
				}
			}
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 获取子账号列表
	 */
	public ResultEntity getAccountList(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、获取子账号列表
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setParentMobile(jp.getMobile());
	    	List<Wxuser> wxuserList = this.wxuserService.list(wxuser);
	    	
	    	for (int i=0; i<wxuserList.size(); i++) {
	    		wxuserList.get(0).setPassword(null);
	    		wxuserList.get(0).setMask(null);
	    		wxuserList.get(0).setParentMobile(null);
	    		wxuserList.get(0).setAdminPower(null);
	    		wxuserList.get(0).setExpireTime(null);
	    		wxuserList.get(0).setSonLimit(null);;
	    	}
	    	
			// 2、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			resultEntity.setWxuserList(wxuserList);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 新增子账号
	 */
	public ResultEntity addAccount(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断创建者是否存在
	    	Wxuser judge1 = new Wxuser();
	    	judge1.setMobile(jp.getMobile());
	    	judge1 = this.wxuserService.get(judge1);
	    	
	    	if (judge1 == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、判断子账号数量是否超标
	    	Wxuser judge2 = new Wxuser();
	    	judge2.setParentMobile(jp.getMobile());
	    	List<Wxuser> wxuserList = this.wxuserService.list(judge2);
	    	
	    	if (!CollectionUtils.isEmpty(wxuserList)) {
	    		if (wxuserList.size() >= judge1.getSonLimit()) {
	    			throw new ServiceException(EnumClass.ResultCodeType.ACCOUNT_LIMIT_ERROR.getKey(), EnumClass.ResultCodeType.ACCOUNT_LIMIT_ERROR.getValue());
	    		}
	    	}
	    	
	    	// 3、判断被创建账号是否已存在
	    	Wxuser judge3 = new Wxuser();
	    	judge3.setMobile(jp.getAccount());
	    	judge3 = this.wxuserService.get(judge3);
	    	
	    	if (judge3 != null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.ACCOUNT_EXIST.getKey(), EnumClass.ResultCodeType.ACCOUNT_EXIST.getValue());
	    	}
	    	
	    	// 4、新增账号
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getAccount());
	    	wxuser.setParentMobile(jp.getMobile());
	    	wxuser.setPassword("0E117EADBADC48F0D354084DCF80FCB3");//默认密码123456
	    	wxuser.setMask("123456");//默认mask123456
	    	wxuser.setName("子账户");
	    	wxuser.setSituation("默认岗位");
	    	wxuser.setOrganName(judge1.getOrganName());
	    	wxuser.setAdminPower(2);
	    	wxuser.setExpireTime(judge1.getExpireTime());
	    	wxuser.setSonLimit(0);
	    	this.wxuserService.save(wxuser);
	    	
			// 5、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 删除子账号
	 */
	public ResultEntity delAccount(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断创建者是否存在
	    	Wxuser judge1 = new Wxuser();
	    	judge1.setMobile(jp.getMobile());
	    	judge1 = this.wxuserService.get(judge1);
	    	
	    	if (judge1 == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、删除账号
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getAccount());
	    	this.wxuserService.delete(wxuser);
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 账号注册(后台)
	 */
	public ResultEntity register(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断是否该账号是否已存在
	    	Wxuser judge = new Wxuser();
	    	judge.setMobile(jp.getAccount());
	    	judge = this.wxuserService.get(judge);
	    	
	    	if (judge != null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.ACCOUNT_EXIST.getKey(), EnumClass.ResultCodeType.ACCOUNT_EXIST.getValue());
	    	}
	    	
	    	// 2、新增账号
	    	Wxuser wxuser = new Wxuser();
	    	wxuser.setMobile(jp.getAccount());
	    	wxuser.setPassword("0E117EADBADC48F0D354084DCF80FCB3");//默认密码123456
	    	wxuser.setMask("123456");//默认mask123456
	    	wxuser.setName(jp.getName());
	    	wxuser.setSituation(jp.getSituation());
	    	wxuser.setOrganName(jp.getOrganName());
	    	wxuser.setAdminPower(1);
	    	Date date = DateUtil.getDateByDay(new Date(), jp.getDays());
	    	wxuser.setExpireTime(new Timestamp(date.getTime()));
	    	wxuser.setSonLimit(2);
	    	this.wxuserService.save(wxuser);
	    	
	    	// 3、复制13999999999账号信息
	    	this.copy139Info(jp.getAccount(), jp.getFlag());
	    	
			// 4、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 更新会员过期时间(后台)
	 */
	public ResultEntity updateExpireTime(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、判断账号是否存在
	    	Wxuser judge = new Wxuser();
	    	judge.setMobile(jp.getAccount());
	    	judge = this.wxuserService.get(judge);
	    	
	    	if (judge == null) {
	    		throw new ServiceException(EnumClass.ResultCodeType.USER_NOT_EXIST.getKey(), EnumClass.ResultCodeType.USER_NOT_EXIST.getValue());
	    	}
	    	
	    	// 2、增加指定天数，需要判断过期时间是否到期
	    	Wxuser update = new Wxuser();
	    	update.setMobile(jp.getAccount());
			long nowTime = System.currentTimeMillis();
			if (judge.getExpireTime() != null && judge.getExpireTime().getTime() >= nowTime) {
				update.setExpireTime(new Timestamp(DateUtil.getDateByDay(new Date(judge.getExpireTime().getTime()), jp.getDays()).getTime()));
			} else {
				update.setExpireTime(new Timestamp(DateUtil.getDateByDay(new Date(), jp.getDays()).getTime()));
			}
	    	
	    	this.wxuserService.update(update);
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	/**
	 * 同步139账号信息(后台)
	 */
	public ResultEntity sync139Info(JsonParam jp) throws Exception {
		
		// 返回结果
		ResultEntity resultEntity = new ResultEntity();
	    // 接口成功返回码
	    int code = EnumClass.ResultCodeType.SUCCESS.getKey();
	    // 返回信息描述
	    String msg = EnumClass.ResultCodeType.SUCCESS.getValue();
		
	    try {
	    	// 1、删除原139账号的信息
    		this.del139Info(jp.getAccount(), jp.getFlag(), jp.getDefaultFlag());
    		
    		// 2、复制139账号的信息
    		this.copy139Info(jp.getAccount(), jp.getFlag());
	    	
			// 3、返回结果
			resultEntity.setCode(code);
			resultEntity.setMessage(msg);
			
			return resultEntity;
			
	    } catch (Exception e) {
	    	throw e;
	    }
        
    }
	
	// 导入清单数据
	public void importData(File file, String mobile) throws Exception  {
		
		FileInputStream in = null;
		
		try {
			Workbook workbook = null;
				
			in = new FileInputStream(file);
			
			// 获取Excel文件
			workbook = Workbook.getWorkbook(in);
			
			// 处理Excel
			//获取第一个工作表sheet
            Sheet sheet = workbook.getSheet(0);
            //获取数据
            String lastCategory = "";
            for (int i = 1; i < sheet.getRows(); i++) {
            	Inventory inventory = new Inventory();
            	String category = sheet.getCell(0, i).getContents();			//类别
            	if (!StringUtils.isEmpty(category)) {
            		lastCategory = category;
            	} else {
            		category = lastCategory;
            	}
            	String projectName = sheet.getCell(1, i).getContents();			//项目名称
            	String price = sheet.getCell(2, i).getContents();				//单价
            	String remark = sheet.getCell(3, i).getContents();				//备注
            	
            	// 判断数据正确性
            	inventory.setCategory(category);
            	// 判断类别
            	if (StringUtils.isEmpty(category.trim())) {
        			throw new ServiceException(EnumClass.ResultCodeType.FAIL.getKey(), "类别不能为空！");
            	}
            	inventory.setMobile(mobile);
            	inventory.setProjectName(projectName);
            	inventory.setPrice(price);
            	inventory.setRemark(remark);
            	inventory.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            	inventory.setSortOrder(i);

            	// 将数据保存到数据库中
            	this.inventoryService.save(inventory);
	        }
            workbook.close();
			
		} catch (Exception e) {
			throw e;
		} finally {
			file.delete();
			if (in != null) {
				in.close();
			}
		}
		
	}
	
	// 复制13999999999账号数据(flag 0:不复制清单 1:复制)
	public void copy139Info(String account, int flag) {
		
		// 1、复制13999999999账号的类别图片库数据
    	CategoryImg categoryImg = new CategoryImg();
    	categoryImg.setMobile("13999999999");
    	List<CategoryImg> categoryImgList = this.categoryImgService.list(categoryImg);
    	
    	for (int i=0; i<categoryImgList.size(); i++) {
    		CategoryImg addImg = new CategoryImg();
    		addImg.setId(UUID.randomUUID().toString().replaceAll("-", ""));
    		addImg.setMobile(account);
    		addImg.setType(categoryImgList.get(i).getType());
    		addImg.setDefaultFlag(1);
    		addImg.setImgUrl(categoryImgList.get(i).getImgUrl());
    		this.categoryImgService.save(addImg);
    	}
    	
    	// 2、复制13999999999账号的类别视频库数据
    	CategoryVideo categoryVideo = new CategoryVideo();
    	categoryVideo.setMobile("13999999999");
    	List<CategoryVideo> categoryVideoList = this.categoryVideoService.list(categoryVideo);
    	
    	for (int i=0; i<categoryVideoList.size(); i++) {
    		CategoryVideo addVideo = new CategoryVideo();
    		addVideo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
    		addVideo.setMobile(account);
    		addVideo.setType(categoryVideoList.get(i).getType());
    		addVideo.setDefaultFlag(1);
    		addVideo.setTitle(categoryVideoList.get(i).getTitle());
    		addVideo.setVideoUrl(categoryVideoList.get(i).getVideoUrl());
    		this.categoryVideoService.save(addVideo);
    	}
    	
    	// 3、复制13999999999账号的类别价格库数据
    	CategoryPrice categoryPrice = new CategoryPrice();
    	categoryPrice.setMobile("13999999999");
    	List<CategoryPrice> categoryPriceList = this.categoryPriceService.list(categoryPrice);
    	
    	for (int i=0; i<categoryPriceList.size(); i++) {
    		CategoryPrice addPrice = new CategoryPrice();
    		addPrice.setId(UUID.randomUUID().toString().replaceAll("-", ""));
    		addPrice.setMobile(account);
    		addPrice.setType(categoryPriceList.get(i).getType());
    		addPrice.setDefaultFlag(1);
    		addPrice.setName(categoryPriceList.get(i).getName());
    		addPrice.setFeature(categoryPriceList.get(i).getFeature());
    		addPrice.setImgUrl(categoryPriceList.get(i).getImgUrl());
    		addPrice.setCountry(categoryPriceList.get(i).getCountry());
    		addPrice.setWarranty(categoryPriceList.get(i).getWarranty());
    		addPrice.setTincture(categoryPriceList.get(i).getTincture());
    		addPrice.setBeauty(categoryPriceList.get(i).getBeauty());
    		addPrice.setWear(categoryPriceList.get(i).getWear());
    		addPrice.setAdvantage1(categoryPriceList.get(i).getAdvantage1());
    		addPrice.setAdvantage2(categoryPriceList.get(i).getAdvantage2());
    		addPrice.setAdvantage3(categoryPriceList.get(i).getAdvantage3());
    		addPrice.setAdvantage4(categoryPriceList.get(i).getAdvantage4());
    		addPrice.setInferior1(categoryPriceList.get(i).getInferior1());
    		addPrice.setInferior2(categoryPriceList.get(i).getInferior2());
    		addPrice.setPrice(categoryPriceList.get(i).getPrice());
    		addPrice.setPriceDesc(categoryPriceList.get(i).getPriceDesc());
    		addPrice.setSelectNum(categoryPriceList.get(i).getSelectNum());
    		addPrice.setSortOrder(categoryPriceList.get(i).getSortOrder());
    		this.categoryPriceService.save(addPrice);
    	}
    	
    	// 4、复制13999999999账号的清单数据
    	if (flag == 1) {
    		Inventory inventory = new Inventory();
        	inventory.setMobile("13999999999");
        	List<Inventory> inventoryList = this.inventoryService.list(inventory);
        	
        	for (int i=0; i<inventoryList.size(); i++) {
        		Inventory addInventory = new Inventory();
        		addInventory.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        		addInventory.setMobile(account);
        		addInventory.setCategory(inventoryList.get(i).getCategory());
        		addInventory.setProjectName(inventoryList.get(i).getProjectName());
        		addInventory.setPrice(inventoryList.get(i).getPrice());
        		addInventory.setRemark(inventoryList.get(i).getRemark());
        		addInventory.setSortOrder(inventoryList.get(i).getSortOrder());
        		this.inventoryService.save(addInventory);
        	}
    	}
	}
	
	// 删除13999999999账号数据(flag 0:不删除清单 1:删除)
	public void del139Info(String account, int flag, int defaultFlag) {
		
		// 1、删除13999999999账号的类别图片库数据
    	CategoryImg categoryImg = new CategoryImg();
    	categoryImg.setMobile(account);
    	if (defaultFlag == 0) {
    		categoryImg.setDefaultFlag(1);
    	}
    	this.categoryImgService.delete(categoryImg);
    	
    	// 2、删除13999999999账号的类别视频库数据
    	CategoryVideo categoryVideo = new CategoryVideo();
    	categoryVideo.setMobile(account);
    	if (defaultFlag == 0) {
    		categoryVideo.setDefaultFlag(1);
    	}
    	this.categoryVideoService.delete(categoryVideo);

    	// 3、删除13999999999账号的类别价格库数据
    	CategoryPrice categoryPrice = new CategoryPrice();
    	categoryPrice.setMobile(account);
    	if (defaultFlag == 0) {
    		categoryPrice.setDefaultFlag(1);
    	}
    	this.categoryPriceService.delete(categoryPrice);
    	
    	// 4、删除13999999999账号的清单数据
    	if (flag == 1) {
    		Inventory inventory = new Inventory();
        	inventory.setMobile(account);
        	this.inventoryService.delete(inventory);
    	}
	}
}
